package com.yonyou.crm.bpub.initData.service;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.initData.entity.InitDatasBodyVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasListItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasVO;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.sprc.lead.entity.LeadVO;
import com.yonyou.crm.sprc.lead.service.LeadBizServiceImpl;

@Service
public class LeadInitDatas extends BaseInitDatas{
	
	@Autowired
	LeadBizServiceImpl service;
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		return (T)service.getDetail(id);
	}
	
	@Override
	public ArrayList<InitDatasListItemVO> getHeader(InitDatasVO initData) {
		ArrayList<InitDatasListItemVO> headerList = new ArrayList<InitDatasListItemVO>();
		InitDatasListItemVO item;
		ArrayList<InitDatasItemVO> itemDataList;
		InitDatasItemVO itemData;
		
		item = new InitDatasListItemVO("id");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("0");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("name");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("gender");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("mobile");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("companyName");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("source");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("industry");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("level");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("tel");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("email");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("website");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("post");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("province_city_district");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("address");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("ownerUserId");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("remarks");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		return headerList;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBody(InitDatasVO initData) {
		ArrayList<InitDatasBodyVO> body = new ArrayList<InitDatasBodyVO>();
		return body;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBodyDefault() {
		ArrayList<InitDatasBodyVO> bodyDefault = new ArrayList<InitDatasBodyVO>();
		return bodyDefault;
	}
	
	public String[] getFieldList() {
		String[] itemkeyList = {"id","name","gender","mobile","companyName","source","industryId","level","tel","email","website","post","province_city_district","address","ownerUserId","remarks"};
		return itemkeyList;
	}
	
	/**
	 * 获取模板配置
	 */
	@Override
	public <T extends Serializable> InitDatasVO getLayout(T obj){
		LeadVO vo = (LeadVO) obj;
		//Long biztypeId = vo.getBiztypeId();
		long biztypeId = 11;
		return layoutService.getInitDatasForMobile(biztypeId);
	}
	
	@Override
	public <T extends Serializable> ArrayList<InitDatasListItemVO> getHeader(T obj, InitDatasVO initData) {
		ArrayList<InitDatasListItemVO> headerList = new ArrayList<InitDatasListItemVO>();
		
		LeadVO vo = (LeadVO)obj;
		
		String[] itemkeyList = getFieldList();
		for (int i = 0; i< itemkeyList.length; i++) {
			InitDatasListItemVO headerItem = new InitDatasListItemVO();
			headerItem.setItemkey(itemkeyList[i]);
			ArrayList<InitDatasItemVO> itemdatalist = new ArrayList<>();
			InitDatasItemVO itemdata = new InitDatasItemVO();
			String dataid = "";
			String datavalue = "";
			
			if ( itemkeyList[i].equals("gender")) {
				dataid =(vo.getGender() == null) ? "" : String.valueOf(vo.getGender());
				datavalue = (vo.getGenderName() == null) ? "" : vo.getGenderName();
			} else if ( itemkeyList[i].equals("source")) {
				dataid =(vo.getSource() == null) ? "" : String.valueOf(vo.getSource());
				datavalue = (vo.getSourceName() == null) ? "" : vo.getSourceName();
			} else if ( itemkeyList[i].equals("industryId")) {
				dataid =(vo.getIndustryId() == null) ? "" : String.valueOf(vo.getIndustryId());
				datavalue = (vo.getIndustryName() == null) ? "" : vo.getIndustryName();
			} else if ( itemkeyList[i].equals("level")) {
				dataid =(vo.getLevel() == null) ? "" : String.valueOf(vo.getLevel());
				datavalue = (vo.getLevelName() == null) ? "" : vo.getLevelName();
			} else if ( itemkeyList[i].equals("post")) {
				dataid =(vo.getPost() == null) ? "" : String.valueOf(vo.getPost());
				datavalue = (vo.getPostName() == null) ? "" : vo.getPostName();
			} else if (itemkeyList[i].equals("ownerUserId")) {
				dataid =(vo.getOwnerUserId() == null) ? "" : String.valueOf(vo.getOwnerUserId());
				datavalue = (vo.getOwnerUserInfo() != null && vo.getOwnerUserInfo().getName() == null) ? "" : vo.getOwnerUserInfo().getName();
			} else if ( itemkeyList[i].equals("province_city_district")) {
				dataid = vo.getProvince()+"_"+vo.getCity()+"_"+vo.getDistrict();
				String province = (vo.getProvinceName() == null) ? "" : vo.getProvinceName();
				String city = (vo.getCityName() == null) ? "" : vo.getCityName();
				String district = (vo.getDistrictName() == null) ? "" : vo.getDistrictName();
				datavalue = province +"_"+ city +"_"+ district;
			} else {
				char[] cs = itemkeyList[i].toCharArray();
				cs[0] = Character.toUpperCase(cs[0]);
				String funcName = String.valueOf(cs);
				try {
					Object data = vo.getClass().getMethod("get"+funcName, new Class[]{}).invoke(vo, new Object[]{});
					dataid = (data == null) ? "" : data.toString();
					datavalue = dataid;
				} catch (IllegalAccessException
						| IllegalArgumentException
						| InvocationTargetException
						| NoSuchMethodException | SecurityException e) {
					throw new CrmBusinessException("动态调用lead方法错误！");
				}
			}
			
			itemdata.setDataid(dataid);
			itemdata.setDatavalue(datavalue);
			itemdatalist.add(itemdata);
			headerItem.setItemdatalist(itemdatalist);
			headerList.add(headerItem);
			
		}
		return headerList;
	}

	@Override
	public <T extends Serializable> ArrayList<InitDatasBodyVO> getBody(T obj, InitDatasVO initData) {
		ArrayList<InitDatasBodyVO> body = new ArrayList<InitDatasBodyVO>();
		return body;
	}
	
}
